﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Newtonsoft.Json;
using AiShow.Model;
using AiShow.BLL;
using AiShow.IBLL;

namespace AiShow.WebSite
{
    public partial class PersonalEdit : System.Web.UI.Page
    {
        private District District;
        private IUserManager UserManager = new UserManager();
  
        private void BindProvince(string ProvinceName) {
            ddlProvince.DataSource = District.Provinces;
            ddlProvince.DataTextField = "ProvinceName";
            ddlProvince.DataValueField = "ProvinceName";
            ddlProvince.DataBind();
            ddlProvince.Items.Insert(0, "-请选择-");
            if (string.IsNullOrEmpty(ProvinceName))
            {
                ddlProvince.SelectedIndex = 0;

            }
            else {
                ddlProvince.SelectedValue = ProvinceName;
            }
        }
        private void BindCity(string ProvinceName,string CityName)
        {
            var  Province = District.Provinces.SingleOrDefault(p => p.ProvinceName == ProvinceName);
            if (Province != null) { 
            ddlCity.DataSource = Province.Citys;
                ddlCity.DataValueField = "CityName";
                ddlCity.DataTextField = "CityName";
                ddlCity.DataBind();
            }
            ddlCity.Items.Insert(0, "-请选择-");
            if (string.IsNullOrWhiteSpace(CityName))
            {
                ddlCity.SelectedIndex = 0;

            }
            else {
                ddlCity.SelectedValue = CityName;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            var file = Server.MapPath("~/App_Data/district.json");
            var text = File.ReadAllText(file,System.Text.Encoding.Default);
            District = JsonConvert.DeserializeObject <District>(text);
            if (!IsPostBack) {

                var ProvinceName = "";
                var CityName = "";
                var user = IdentityManager.ReadUser();
                if (user == null)
                {
                    Response.Redirect("login.aspx?RetrnUrl=PersonalEdit.aspx");
                }
                else {
                    
                    if (!string.IsNullOrWhiteSpace(user.Address)) { 
                    string[] addressArray = user.Address.Split(' ');
                    if (addressArray.Length == 2) {
                        ProvinceName = addressArray[0];
                            CityName = addressArray[1];
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(user.Hobby)) {
                        string[] hobbys = user.Hobby.Split(',');
                        foreach (ListItem item in cblHobby.Items) {
                            var hobby = item.Text;
                            item.Selected = hobbys.Any(p => p==hobby);
                        }
                    }
                    txtNickName.Text = user.NickName;
                    txtBirthday.Text = user.Birthday?.ToString("yyyy-MM-dd");
                    ddlSex.SelectedValue = user.Sex.ToString();
                }
                BindProvince(ProvinceName);
                BindCity(ProvinceName,CityName);
            }

        }

        protected void btnProfile_Click(object sender, EventArgs e)
        {
            var user = IdentityManager.ReadUser();
            {
                user.NickName = txtNickName.Text;
                user.Address = ddlProvince.SelectedValue + "" + ddlCity.SelectedValue;
                user.Birthday = Convert.ToDateTime(txtBirthday.Text);
                user.Sex = Convert.ToInt32(ddlSex.SelectedValue);

                string hobby = "";
                foreach (ListItem item in cblHobby.Items)
                {
                    if (item.Selected)
                    {
                        hobby += item.Text + ",";
                    }
                }
                if (hobby.Length > 1)
                    user.Hobby = hobby.Substring(0, hobby.Length - 1);
                else 
                    user.Hobby = hobby;

                var result = UserManager.EditWithoutAvatar(user);
                if (result.StatusCode == Model.OperResult.StatusCode.Succeed)
                {
                    Response.Redirect("Personal.aspx");
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "editerror", $"alert('{result.Message}')", true);
                }
            }
        }
        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            var Province = ddlProvince.SelectedValue;
            BindCity(Province, "");
        }
    }
}